package com.authspere.plugin.global;

import com.authsphere.common.constant.Constants;
import com.authsphere.common.enums.PluginBeforeEnum;
import com.authsphere.plugin.api.AuthSphereBeforePlugin;
import com.authsphere.plugin.api.AuthSphereBeforePluginChain;
import com.authsphere.plugin.api.WebExchange;
import com.authsphere.plugin.api.context.AuthSphereContext;
import com.authsphere.plugin.api.context.AuthSphereContextBuilder;

/**
 * @program: AuthSphere
 * @description:
 * @author: YuKai Fan
 * @create: 2025/3/25 16:11
 **/
public class GlobalPlugin implements AuthSphereBeforePlugin {

    private final AuthSphereContextBuilder builder;

    public GlobalPlugin(AuthSphereContextBuilder builder) {
        this.builder = builder;
    }

    @Override
    public void execute(WebExchange exchange, AuthSphereBeforePluginChain chain) {
        AuthSphereContext context = builder.build(exchange);
        exchange.getRequest().setAttribute(Constants.CONTEXT, context);
        chain.execute(exchange);
    }

    @Override
    public int getOrder() {
        return PluginBeforeEnum.ACCOUNT_LOCK.getCode();
    }
}
